<template>
  <div class="menu">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button @click="$router.push({ name: 'menu-create' })"
          >添加菜单</el-button
        >
      </div>
      <el-table :data="menus" style="width: 100%">
        <el-table-column type="index" label="编号" min-width="150px"> </el-table-column>
        <el-table-column prop="name" label="菜单名称" min-width="150px"> </el-table-column>
        <el-table-column prop="level" label="菜单级数" min-width="150px"> </el-table-column>
        <el-table-column prop="icon" label="前端图标" min-width="150px"> </el-table-column>
        <el-table-column prop="orderNum" label="排序" min-width="150px"> </el-table-column>
        <el-table-column label="操作" min-width="150px">
          <template slot-scope="scope">
            <el-button @click="handleEdit(scope.row)" type="text" size="small"
              >编辑</el-button
            >
            <el-button @click="handleDelete(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getAllMenus, deleteMenu } from '@/services/menu'

export default Vue.extend({
  name: 'Menu',
  data () {
    return {
      menus: [
        {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        },
        {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        },
        {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄'
        }
      ]
    }
  },
  methods: {
    async loadAllMenus () {
      const { data } = await getAllMenus()
      console.log(data)
      if (data.code === '000000') {
        this.menus = data.data
      }
    },
    handleEdit (item:any) {
      console.log('handleEdit')
      this.$router.push({
        name: 'menu-edit',
        params: {
          id: item.id
        }
      })
    },
    handleDelete (item:any) {
      this.$confirm('确认删除吗?', '删除提示', {}).then(async () => {
        // 确认执行这里
        const { data } = await deleteMenu(item.id)
        if (data.code === '000000') {
          this.$message.success('删除成功')
          this.loadAllMenus() // 更新数据列表
        }
      }).catch(() => {
        // 取消执行这里
        this.$message.info('取消删除')
      })
    }
  },
  async created () {
    this.loadAllMenus()
  }
})
</script>
